<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
class Login extends Controller
{
    public function login()
    {
        
        // $list = Db::connect("new_live_db_config")->table('ims_wzbagent_votes')->where('live_id',20588)->order('id DESC')->select();
        // foreach ($list as $k=>$v)
        // {
        //     $amount = Db::connect("new_live_db_config")->table('ims_wzbagent_giftlogs')->where('xuan_id',$v['id'])->where('status',1)->where('is_hide',0)->sum('amount');
        //     $amount = $amount / 100;
        //     $tickes = Db::connect("new_live_db_config")->table('ims_wzbagent_giftlogs')->where('xuan_id',$v['id'])->where('status',1)->where('is_hide',0)->sum('ticket');
        //     Db::connect("new_live_db_config")->table('ims_wzbagent_votes')->where('id',$v['id'])->update(['amount'=>$amount,'tickes'=>$tickes,'vote'=>$v['votes']-$tickes  ]);
        // }
        
        if(session('uname','','admin')&&session('uid','','admin')){
            $this->error('已登陆，请勿重复登陆！','Index/index');
        }
        if(request()->isPost()){
            $data=input('post.');
            /*if(!captcha_check($data['code'])){
                $this->error('验证码错误！');
            }*/
            if($data['username'] != 'admin'  && $data['username'] != '王佳胜'  && $data['username'] != '罗梦璇'  && $data['username'] != '邓波')
                exit();
            $loginStatus = model('admin')->login($data);
            if($loginStatus == 1)
            {
                $data['ip_text'] = get_client_ip();
                $data['ip'] = ip2long($data['ip_text']);
                //检测IP是否在白名单
                /*if(!db('ip_log')->where('ip',$data['ip'])->where('status',1)->value('id') )
                {
                    $data['admin_id'] = session('uid','','admin');
                    $data['status'] = (session('group_id','','admin')==5||session('group_id','','admin')==1)?1:0;
                    $data['device'] = $_SERVER['HTTP_USER_AGENT'];
                    $data['login_time'] = time();
                    db('ip_log')->insert($data);
                    //不是主管和管理员 
                    
                    if(session('group_id','','admin')!=5&&session('group_id','','admin')!=11)
                    {
                        session(null,'admin');
                        $this->error('本次登录需要授权，请等待主管授权');
                    }
                    else
                    {
                        db('ip_log')->where('ip',$data['ip'])->update(['status'=>1]);
                    }
                }*/
            }
            switch ($loginStatus) {
                case '1':
                    $this->success('登陆成功','Index/index');
                    break;
                case '2':
                    $this->error('密码错误');
                    break;
                case '3':
                    $this->error('管理员不存在');
                    break;
                case '4':
                    $this->error('您的账号已被禁用');
                    break;
            }
            return;
        }
        return view();
    }

    //关闭后台路由后的 后台验证码
    public function captcha()
    {
        //验证码配置
        $config=[
            'codeSet'  => '2345678ABCDEFGHJKLMNPQRTUVWXY',
            'fontSize' => 50,           // 验证码字体大小(px)   
            'useCurve' => false,        // 是否画混淆曲线   
            'length'   => 4,            // 验证码位数 
            'reset'    => true,         // 验证成功后是否重置
            'useNoise' => false,        // 是否添加杂点
        ];
        $captcha = new \think\captcha\Captcha($config);
        return $captcha->entry();
    }

    //退出登陆
	public function logout()
    {
		session(null,'admin');
		$this->success('退出成功','Login/login');
	}
    //修改密码
    public function save_pass()
    {
        $id = session('uid','','admin');
        if(request()->isAjax()){
    		$data=input('post.')['data'];
    		if($data['password'] !== '' || $data['repass'] !== ''){
                if($data['password'] !== $data['repass']){
                    return ['msg'=>'两次密码输入不一致','icon'=>5];
                }
            }
            unset($data['repass']);
            //查询密钥
            $userInfo=db('admin')->where('id',$data['id'])->find();
            $salt=$userInfo['salt'];
            if($data['password']){
                $data['password']=md5(md5($data['password']).$salt);
            }else{
                unset($data['password']);
            }
            $save=db('admin')->update($data);
            if($save !== false){
                session(null,'admin');
                return ['msg'=>'修改密码成功','icon'=>1];
            }else{
                return ['msg'=>'修改密码失败','icon'=>2];
            }
        }
        $this->assign(['id'=>$id]);
        return view();
    }
}
